package com.kgc.utils;
import com.kgc.common.Dto;
import com.kgc.common.DtoUtil;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.regex.Pattern;
@Component
public class PhoneOrEmailRegex {
    public static final String EMAIL_REGEX = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    public static final String PHONE_REGEX="^[1][3,4,5,7,8][0-9]{9}$";
    public Dto regexPhoneOrEmail(String phoneoremail){
        if (phoneoremail==null){
            return DtoUtil.returnFail("请输入手机号","1004");
        }
        ArrayList<Character> list=new ArrayList<>();
        for (Character c:phoneoremail.toCharArray()){
            list.add(c);
        }
        if (list.stream().anyMatch(c -> c.equals('@') || c.equals('.'))) {
            if (Pattern.compile(PHONE_REGEX).matcher(EMAIL_REGEX).matches()){
                return DtoUtil.returnSuccess("true");
            }else {
                return DtoUtil.returnSuccess("请输入正确的邮箱格式");
            }
        }else
        if (phoneoremail!=null){
            if (phoneoremail.length() != 11){
                return DtoUtil.returnFail("请输入大陆手机号","1003");
            }
            if (Pattern.compile(PHONE_REGEX).matcher(phoneoremail).matches()) {
                return DtoUtil.returnSuccess("true");
            }else {
                return DtoUtil.returnSuccess("请输入正确的手机号格式");
            }
        }
        return DtoUtil.returnFail("请输入手机号","1004");
    }
    public String getFourRandom(){
        StringBuffer sb=new StringBuffer();
        String v =Math.random() * 10000+"";
        for (Character c:v.toCharArray()){
            if (c.equals('.')){
                break;
            }
            sb.append(c);
        }
        return sb.toString();
    }
}
